//
//  SSGetCouponViewController.swift
//  拾舍清洁
//
//  Created by sky.wei on 2023/6/1.
//

import UIKit

import SwiftyJSON


class SSGetCouponViewController: UIViewController {
    
    @IBOutlet weak var closebtn: UIButton!
    @IBOutlet weak var tableView: UITableView!
//    var items = [SSCoupon]()
    var myCouponClosure: MyCouponClosure?     // 闭包

    override func viewDidLoad() {
        super.viewDidLoad()

        tableView.register(UINib(nibName: "SSCouponTableViewCell", bundle: Bundle.init(for: SSCouponTableViewCell.self)), forCellReuseIdentifier: "SSCouponTableViewCell")
        tableView.delegate = self
        tableView.dataSource = self
        tableView.separatorStyle = .none
        closebtn.ssc_expandSize(size: 50)
        network()
    }
    
    var items: [SSBanner] = [] {
        didSet {
            self.tableView.reloadData()
        }
    }
    
//    func setData(data:[SSCoupon])  {
//        self.items = data
//        self.tableView.reloadData()
//    }
    func closure(_ finished: @escaping MyCouponClosure) {
        myCouponClosure = finished
    }
    
    @IBAction func closeAction(_ sender: Any) {
        dismiss(animated: true)
    }
    
    func network() {
        //banner
        SSNetwrokManager.share.request(urlString: APIUrl.share.getCouponItem_URL,isLoading: false,isAlert: false) { result in
            guard let banners = [SSBanner].deserialize(from: result, designatedPath:"data.listCoupon") else {
                return
            }
            self.items = banners as! [SSBanner]
        }
    }
    
    
    func getAddCouponNetwork(id:String) {
        let param = [
            "couponId": id,
            "phoneNo": kUserPhone
        ]as[String:AnyObject]
        SSProgressHUD.share.show()
        HttpConnectTools.share.request(urlString: APIUrl.share.addCoupon_URL, parameters: param) { (result, error) in
            SSProgressHUD.share.dismiss()
            if let _ = result{
                let json = JSON(parseJSON: result!)
                self.dismiss(animated: true)
               let view = SSGetCouponDoneView.couponDoneView()
                view.numberResult {
                    let vc = SSMyCouponController.init()
                    UIViewController.current().navigationController?.pushViewController(vc, animated: true)
                }

            }else{
                if let message = error?.userInfo["errorinfo"]{
                    UIViewController.current().view.makeToast(message as? String)
                }
            }
        }
    }

  

}
extension SSGetCouponViewController: UITableViewDelegate, UITableViewDataSource
{
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let banner:SSBanner = self.items[indexPath.row] as! SSBanner
        let alertView = EtcAlertView(title:"领取优惠券", message: "您是否要领取优惠券", cancleTitle:"取消", confirmTitle: "确定")
        alertView.showAlert(confirm:  {
            self.getAddCouponNetwork(id: banner.id)
        }) {
        }
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 89 + 16
    }
  
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return items.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "SSCouponTableViewCell") as! SSCouponTableViewCell
        cell.model = self.items[indexPath.row]
//        cell.setModel(model: self.items[indexPath.row] )
        return cell
    }
    
   
}
